﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using NLBS.Entity;
using System.Data;
using NHibernate.Criterion;

namespace NLBS.Data
{
    public partial class NHibernateDataProvider : DbProvider
    {
        #region trackback

        public override IList<TrackBack> GetTrackBack()
        {
            return CurrentSession.CreateCriteria(typeof(TrackBack)).List<TrackBack>();
        }

        public override IPagedCollection<TrackBack> GetTrackBackBySearch(int startIndex, int pageSize, string prefix)
        {
            IList<TrackBack> list= CurrentSession.CreateCriteria(typeof(TrackBack))
                .Add(Expression.Like("Title",prefix))
                .SetFirstResult(startIndex)
                .SetMaxResults(pageSize).List<TrackBack>();

            StringBuilder sb = new StringBuilder();
            string[] prefixArray = prefix.Split(new char[] { ' ' });

            for (int i = 0; i < prefixArray.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append("AND ");
                }
                sb.AppendFormat("tb_title LIKE '%{0}%'", prefixArray[i]);
            }
            
            DbCommand dbCommand = db.GetSqlStringCommand(string.Format(@"SELECT top {0} tb.*, 
            tLog.log_authorID, tLog.log_title,tLog.log_catID
            FROM [blog_Article] tLog, [TrackBack] tb
            WHERE  tLog.log_id=tTB.log_id and tb_id<=(select min(tb_id) 
            from (select top {1} tb_id from TrackBack order by tb_id desc) as T) AND {2} 
            ORDER BY tb_time DESC;
            SELECT COUNT(tb_id) AS TotalRecords from TrackBack where {2}", startIndex, pageSize, sb.ToString()));
            IDataReader dr = db.ExecuteReader(dbCommand);
            IPagedCollection<TrackBack> lists = new PagedCollection<TrackBack>(list);

            lists.MaxItems =Convert.ToInt32(CurrentSession.CreateQuery("SELECT COUNT(ID) from TrackBack where {2}").UniqueResult());
            return lists;
        }

        public override void InsertTrackBack(TrackBack tb)
        {
            CurrentSession.Save(tb);
        }

        public override TrackBack GetTrackBackByID(int id)
        {
            return CurrentSession.Get<TrackBack>(id);

        }

        public override TrackBack GetTrackBackByName(string title, string excerpt)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("select * from blog_Trackback where tb_title=@title and tb_excerpt=@excerpt");
            db.AddInParameter(dbCommand, "title", DbType.String, title);
            db.AddInParameter(dbCommand, "excerpt", DbType.String, excerpt);
            TrackBack tb = null;
            IDataReader dr = db.ExecuteReader(dbCommand);
            if (dr.Read())
            {
                tb = CommonDataProvider.PopulateTrackBackFromIDataReader(dr);
            }
            return tb;
        }

        public override void UpdateTrackBack(TrackBack tb)
        {

        }

        public override void DeleteTrackBack(int id)
        {
            CurrentSession.Delete(GetTrackBackByID(id));
            CurrentSession.Flush();
        }

        #endregion
    }
}
